require(['grid', 'topBar', 'layout','uploadify', 'date'], function () {
    var postData,rcx,type,orgIds,$grid = $('#grid'),search,getEditDialog,editDialog,funName,$drvExamFile = $("#drvExamFile");
	function topBarInit() {
		$('#top').topBar({
			left: [{
				type: 'filter-more',
				dateType: 'custom',
				search: function () {
					search();
				},
				item: [
					{
						text: '时间快捷',
						name1: 'beginTime',
						name2: 'endTime',
						type: 'dateLine',
//						hidden: true
					},
					{
						text: '按条件汇总',
						name: 'sumTimeType',
						type: 'item-select',
						data: [
							{text: '按日汇总', value: '10'},
							{text: '按月汇总', value: '7'},
							{text: '总体汇总', value: ''}
						]
					}
				]
			}
			],
			right: [
				{
					text: '导出',
					type: 'export-btn',
					authority: '',
					item: [
						{
							text: 'Excel导出',
							click: function () {
								exportFile('Excel');
							}
						},
						{
							text: 'PDF导出',
							click: function () {
								exportFile('Pdf');
							}
						}
					]
				},
				{
					text: '错误信息',
					type: 'btn',
					width: '698',
					authority: '',
					click: function () {
						var data=commonFun.getLeftTreeCheckedNodes('LINE',true);
						var corpId=data[0].getParentNode().corpId;
						getEditDialog3('./service/setting/funDriverExamErrInfo.htm', 'examErrInfo', '错误信息查看').dialog('exec', 'examErrInfo',corpId);
					}
				},
				{
					text: '导入',
					type: 'btn',
					width: '698',
					authority: '',
					click: function () {
						var data=commonFun.getLeftTreeCheckedNodes('LINE',true);
						
						if(data==undefined){
							$.messager.alert("提示", "请选择企业!", "warning");
							return false;
						}
						var corpId=data[0].getParentNode().corpId;
						getEditDialog2('./service/setting/funDriverExamUpload.htm','选择导入文件').dialog('exec', 'importDriverExam',corpId);
					}
				},{
					text: '导入模版',
					type: 'btn',
					width: '698',
					authority: '',
					click: function () {
						downloadDrvExamImpTemplate();
					}
				},{
					text: '删除',
					type: 'btn',
					width: '698',
					authority: '',
					click: function () {
						delDrvExamBnt();
					}
				},{
					text: '新增',
					type: 'btn',
					width: '698',
					authority: '',
					click: function () {
						var lineIds = commonFun.getLeftTreeCheckedNodes().LINE;
						if(typeof(lineIds) == 'undefined'){ // 左侧树没有选中线路
							$.messager.alert("提示","请选中一条线路操作。","info");
						}else if(lineIds.length > 1){ // 左侧树选中车辆大于一辆
							$.messager.alert("提示","您一次只能选中一条线路操作。","info");
						}else {
							getEditDialog('./service/setting/funDriverExamSetAdd.htm', 'add', '新增').dialog('exec', 'reset',lineIds);
						}
					}
				}
			]
		});
	}
    //获取左侧选择的节点信息
    function getPostData() {
        rcx = commonFun.getTreeCheckNodeToArgs();
        type = commonFun.getLeftTreeCurrentTreeType();
		orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
            postData.type = type;
            
            postData.orgIds = orgIds;
            
        }
        return rcx;
    }

    function exportFile(suffix) {
        if (getPostData()) {
            if ($grid.getGridParam("reccount") < 1) {
                $.messager.alert("提示", "请查询数据。", "info");
                return;
            }
            var url = "/baseDriverExamInfo/exportBaseDriverExam2" + suffix + ".htm";
            $.exportFile(url, postData);
        }
        if ($grid.getGridParam("reccount") > 0) {
            if (getPostData()) {
                var url = "/baseDriverExamInfo/exportBaseDriverExam2" + suffix + ".htm";
                $.exportFile(url, postData);
            }
        }
    }
    //grid初始化
    function gridInit() {
		$grid.grid({
	        url: "/baseDriverExamInfo/searchDriverExamInfoPage.htm",
	        colModel: [
	            {name: "attendId", label: "attendId", hidden: true},
	            {name: "lineName", label: "运营线路"},
	            {name: "drvName", label: "驾驶员"},
	            {name: "timecardCardno", label: "考勤卡卡号"},
	            {name: "carName", label: "车牌号"},
	            {name: "carInCd", label: "车辆编号"},
	            {name: "signInTime", label: "签到时间"},
				{name: "signOutTime", label: "签退时间"},
				{name: "workDura", label: "工作时长"},
				{name: "operate", width: 130, label: '操作', title: false,align:'center',
					formatter: 'operArea',
					formatoptions: [{
						text: '编辑',
						css:'operate-edit',
						click: function (data) {
							getEditDialog('./service/setting/funDriverExamSetEdit.htm','edit','编辑').dialog('exec', 'setVal',data);
						}
					}, {
						text: '删除',
						css:'operate-delete',
						click: function (data) {
							$.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
								if (r) {
									$.ajaxExt({
										url : "/baseDriverExamInfo/delDriverExamInfo.htm",
										data : {'attendId' : data.attendId}
									}).done(function(){
										$grid.trigger("reloadGrid");
									});
								}
							});
						}
					}]
				}
	        ]
	    });
    }

	/**
	 * 获取表单对话框，如果没有就先创建
	 * @param option {object} 对话框配置
	 * @returns {jQuery}
	 */
	function getEditDialog(url,flag,option) {
		funName = flag;
		/*option = option || {};
		if (editDialog) {
			editDialog.dialog('open');
			if (option.title) {
				editDialog.dialog('setOption', 'title', option.title);
			}
		} else {*/
			editDialog = $('<div>').dialog({
				title: option.title || '驾驶员考勤',
				url: url,
				height: 550,
				width: 700,
				autoOpen: true,
				buttons: [
					{
						text: '保存',
						css: 'mr10',
						click: function () {
							var _t = this;
							//funName :  add or edit
							$(_t).dialog('exec',funName,function(){
//								reload(true);
							});
						}
					},
					{
						text: '关闭',
						css: 'btn-cancel',
						click: function () {
							$(this).dialog('close');
						}
					}
				]
			});
		//}
		return editDialog;
	}

	function getEditDialog2(url,option) {
		editDialog = $('<div>').dialog({
			title: option.title || '驾驶员考勤',
			url: url,
			height: 300,
			width: 600,
			autoOpen: true,
			buttons: [
				{
					text: '关闭',
					css: 'btn-cancel',
					click: function () {
						$(this).dialog('close');
					}
				}
			]
		});
		return editDialog;
	}

	function getEditDialog3(url,flag,option) {
		funName = flag;
		editDialog = $('<div>').dialog({
			title: option.title || '错误信息查看',
			url: url,
			height: 600,
			width: 1330,
			autoOpen: true,
			buttons: [
				{
					text: '重新上传',
					css: 'mr10',
					click: function () {
						var _t = this;
						$(_t).dialog('exec', 'loadAgagin',function(){
							reload(true);
						});
					}
				},
				{
					text: '关闭',
					css: 'btn-cancel',
					click: function () {
						$(this).dialog('close');
					}
				}
			]
		});
		return editDialog;
	}

	function search() {
        if (getPostData()) {
			$grid.grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    }

	//新增修改后执行
	function reload(close){
		search();
		close && editDialog.dialog('close');
	};

	// 批量删除
	function delDrvExamBnt() {
		var attendIds = $grid.grid("getGridParam", "selarrrow");
		var ids = [];
		for (var i = 0; i < attendIds.length; i ++) {
			var rowData = $grid.getRowData(attendIds[i]);
			ids.push(rowData.attendId);
		}
		if(attendIds.length == 0){
			$.messager.alert("提示","请勾选删除项，进行批量删除！","info");
		}else{
			$.messager.confirm("是否删除","是否批量删除驾驶员考勤信息？",function(r){
				if(r){
					$.ajaxExt({
						url : "/baseDriverExamInfo/delDriverExamInfoByAttendIds.htm",
						data : { 'attendIds': ids }
					}).done(function(){
						$grid.trigger("reloadGrid");
					});
				}
			});
		}
	}
	// 导入模版的下载
	function downloadDrvExamImpTemplate() {
		var url =  "/baseDriverExamInfo/downloadDrvExamImptemplate.htm";
		$.exportFile(url, postData);
		//$("#downloadDrvExamImpTemplateForm").attr("action",url);
		//$("#downloadDrvExamImpTemplateForm").submit();
	}

	//layout 布局
	$('body').layout();
	topBarInit();
	gridInit();
});